package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.PinyinUtils;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.SettingsBiz;
import com.egao.orderB.entity.Settings;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2020-12-16 16:28:35
 */
@Repository
public class SettingsBizImpl extends BaseDao implements SettingsBiz {

  @Override
  public JsonResult list(PageResult2<T> page, Settings settings, User loginUser) throws Exception {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<Settings> rm = new BeanPropertyRowMapper<Settings>(Settings.class);
    StringBuffer sql = new StringBuffer("select s.id,s.organization_id,s.name,s.pinyin");
    sql.append(",s.english_name");
    sql.append(",s.value,s.type,s.state,s.create_time");
    sql.append(",s.create_user,s.update_time,s.update_user");
    sql.append(",suser.username createName ");
    sql.append(" from chf_settings s ");
    sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
    sql.append(" where s.deleted=0 ");
    if (Utils.isNotEmpty(settings)) {
      if (Utils.isNotEmpty(settings.getName())) {
        sql.append(" and (instr(s.name,:name)>0  or instr(s.pinyin,:name)>0 ) ");
        map.put("name", settings.getName());
      }
      if (Utils.isNotEmpty(settings.getType())) {
        sql.append(" and instr(s.type,:type)>0 ");
        map.put("type", settings.getType());
      }
      if (Utils.isNotEmpty(settings.getState())) {
        sql.append(" and instr(s.state,:state)>0 ");
        map.put("state", settings.getState());
      }
      if (Utils.isNotEmpty(settings.getSort()) && Utils.isNotEmpty(settings.getOrder())) {
        sql.append(" order by s." + settings.getSort() + " " + settings.getOrder());
      } else {
        sql.append(" order by s.create_time desc");
      }
    }
    return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
  }

  @Override
  public List<Settings> list(Settings settings, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<Settings> rm = new BeanPropertyRowMapper<Settings>(Settings.class);
    StringBuffer sql = new StringBuffer("select s.id,s.organization_id,s.name,s.pinyin");
    sql.append(",s.english_name");
    sql.append(",s.value,s.type,s.state,s.create_time");
    sql.append(",s.create_user,s.update_time,s.update_user");
    sql.append(",suser.username createName ");
    sql.append(" from chf_settings s ");
    sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
    sql.append(" where s.deleted=0 ");
    if (Utils.isNotEmpty(settings)) {
      if (Utils.isNotEmpty(settings.getName())) {
        sql.append(" and instr(s.name,:name)>0 ");
        map.put("name", settings.getName());
      }
      if (Utils.isNotEmpty(settings.getType())) {
        sql.append(" and instr(s.type,:type)>0 ");
        map.put("type", settings.getType());
      }
      if (Utils.isNotEmpty(settings.getState())) {
        sql.append(" and instr(s.state,:state)>0 ");
        map.put("state", settings.getState());
      }
      if (Utils.isNotEmpty(settings.getSort()) && Utils.isNotEmpty(settings.getOrder())) {
        sql.append(" order by s." + settings.getSort() + " " + settings.getOrder());
      } else {
        sql.append(" order by s.create_time desc");
      }
    }
    getMapParameter(sql, map);
    return jdbcTemplate.query(sql.toString(), map, rm);
  }

  @Override
  public Settings getById(Settings settings) {
    RowMapper<Settings> rm = new BeanPropertyRowMapper<Settings>(Settings.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select s.id,s.organization_id,s.name,s.pinyin");
    sql.append(",s.english_name");
    sql.append(",s.value,s.type,s.state,s.create_time");
    sql.append(",s.create_user,s.update_time,s.update_user");
    sql.append(",suser.username createName ");
    sql.append(" from chf_settings s ");
    sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
    sql.append(" where s.deleted=0  and s.id=:id ");
    map.put("id", settings.getId());
    getMapParameter(sql, map);
    return jdbcTemplate.queryForObject(sql.toString(), map, rm);
  }

  @Override
  public Settings getByName(Settings settings) {
    RowMapper<Settings> rm = new BeanPropertyRowMapper<Settings>(Settings.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select s.id,s.organization_id,s.name,s.pinyin");
    sql.append(",s.english_name");
    sql.append(",s.value,s.type,s.state,s.create_time");
    sql.append(",s.create_user,s.update_time,s.update_user ");
    sql.append(" from chf_settings s ");
    sql.append(" where s.deleted=0 and s.name=:name ");
    map.put("name", settings.getName());
    getMapParameter(sql, map);
    return jdbcTemplate.queryForObject(sql.toString(), map, rm);
  }

  @Override
  public boolean save(Settings settings, User loginUser) {
    settings.setPinyin(PinyinUtils.ToFirstChar(settings.getName()));
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("insert into chf_settings (");
    sql.append("organization_id,name,english_name,pinyin,value,type");
    sql.append(",state,create_user,create_time) values ( ");
    sql.append(":organization_id,:name,:english_name,:pinyin,:value,:type");
    sql.append(",:state,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("name", settings.getName());
    map.put("english_name", settings.getEnglishName());
    map.put("pinyin", settings.getPinyin());
    map.put("value", settings.getValue());
    map.put("type", settings.getType());
    map.put("state", settings.getState());
    map.put("create_user", loginUser.getUserId() + "");
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean update(Settings settings, User loginUser) {
    settings.setPinyin(PinyinUtils.ToFirstChar(settings.getName()));
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_settings set ");
    sql.append("name=:name,english_name=:english_name,pinyin=:pinyin,value=:value ");
    sql.append(",state=:state,update_time=now(),update_user=:update_user where id=:id ");
    map.put("name", settings.getName());
    map.put("english_name", settings.getEnglishName());
    map.put("pinyin", settings.getPinyin());
    map.put("value", settings.getValue());
    map.put("type", settings.getType());
    map.put("state", settings.getState());
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", settings.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean updateOilFee(Settings settings, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_settings set ");
    sql.append("value=:value ");
    sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("value", settings.getValue());
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", settings.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean remove(Settings settings, User loginUser) {
    int count = 0;
    Map<String, String> map = null;
    if (settings.getId().length() > 0) {
      for (String ID : settings.getId().split(",")) {
        map = new HashMap<String, String>();
        if (ID == null || ID.equals("")) {
          break;
        }
        StringBuffer sql = new StringBuffer("update chf_settings set deleted=1" +
                ",update_user=:update_user, update_time=now() "
                + " where id =:id ");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", ID);
        getMapParameter(sql, map);
        count = jdbcTemplate.update(sql.toString(), map);
      }
    }
    return count > 0;
  }
}
